import java.io.*;
import java.sql.*;
import java.util.*;

public class ImageDownLoader {
    public static void main(String[] args) throws IOException, SQLException {
        if (args.length < 2) {
            System.err.println("Usage: java ImageUpLoader <outfile> <uid>");
            System.exit(1);
        }

        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", "postgres");
        connectionProps.put("password", "derp");

        conn = DriverManager.getConnection("jdbc:postgresql:kalender", connectionProps);

        File file = new File(args[0]);
        FileOutputStream fos = new FileOutputStream(file);
        PreparedStatement ps = conn.prepareStatement("SELECT * FROM luserpic WHERE uid = ?");
        ps.setInt(1, Integer.parseInt(args[1]));
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            byte[] data = rs.getBytes("picture");
            fos.write(data);
            fos.close();
        }
        ps.close();
    }
}
